<%@page import="java.util.*"%>
<%@page import="java.text.DecimalFormat"%>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="com.cdvtc.flower.model.Customer" %>
<%@ page import="com.cdvtc.flower.model.CartItem" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	DecimalFormat dcf = new DecimalFormat("0.00");
	request.setAttribute("dcf", dcf);
	Customer customer = (Customer) request.getSession().getAttribute("customer");
	System.out.println(customer);
	if (customer != null) {
		request.setAttribute("customer", customer);
	}
	List<CartItem> cart = (List<CartItem>)session.getAttribute("cart");
	// 计算总金额
	BigDecimal total = BigDecimal.ZERO;
	if (cart != null) {
		for (CartItem cartItem : cart) {
			total = total.add(cartItem.getTotal()); //累加汇总
		}
	}
	pageContext.setAttribute("total", total);
%>
<!DOCTYPE html>
<html>
<head>
	<style>
		.table tbody td {
			vertical-align: middle;
		}

		.btn-incre, .btn-decre {
			box-shadow: none;
			font-size: 25px;
		}
	</style>
</head>
<body>
<%@include file="/commons/navbar.jsp"%>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/all.min.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<script src="${pageContext.request.contextPath}/js2/jquery-3.2.1.slim.min.js"></script>
<script src="${pageContext.request.contextPath}/js2/popper.min.js"></script>
<script src="${pageContext.request.contextPath}/js2/bootstrap.min.js"></script>
<div class="container my-3">
	<div class="d-flex py-3"><h3>总金额: ￥${dcf.format(total)} </h3> <a class="mx-3 btn btn-primary" href="cart-check-out"><i class="fas fa-cash-register">结账</i></a></div>
	<table class="table table-light">
		<thead>
		<tr>
			<th scope="col">商品名称</th>
			<th scope="col">价格</th>
			<th scope="col">数量</th>
			<th scope="col">立即购买</th>
			<th scope="col">取消</th>
		</tr>
		</thead>
		<tbody>
		<%
			if (cart != null) {
				for (CartItem item : cart) {
		%>
		<tr>
			<td><%=item.getProduct().getProduct_name()%></td>
			<td><%= dcf.format(item.getProduct().getSale_price())%></td>
			<td>
				<form action="order-now" method="get" class="form-inline">
					<input type="hidden" name="product_id" value="<%=item.getProduct().getProduct_id()%>" class="form-input">
					<div class="form-group d-flex justify-content-between">
						<a class="btn bnt-sm btn-incre" href="quantity-inc-dec?action=inc&id=<%=item.getProduct().getProduct_id()%>"><i class="fas fa-plus-square"></i></a>
						<input type="text" name="quantity" class="form-control"  value="<%=item.getQuantity()%>" readonly>
						<a class="btn btn-sm btn-decre" href="quantity-inc-dec?action=dec&id=<%=item.getProduct().getProduct_id()%>"><i class="fas fa-minus-square"></i></a>
					</div>
					<button type="submit" class="btn btn-primary btn-sm">购买</button>
				</form>
			</td>
			<td><a href="remove-from-cart?id=<%=item.getProduct().getProduct_id() %>" class="btn btn-sm btn-danger">删除</a></td>
		</tr>

		<%
				}}%>
		</tbody>
	</table>
</div>
</body>
</html>
